<?php
require_once(Kohana::find_file('vendor','phprpc/phprpc_client'));
/**
 * 从供应商读取供应商银行账号信息同步到进销存
 * Created by PhpStorm.
 * User: locove
 * Date: 14-10-29
 * Time: 下午7:17
 */
class Acsystemclient
{
    //const SERVER_NAME='http://127.0.0.1:808/commonapi/acsystem/newerpapi/';
    //const SERVER_NAME='http://192.168.5.59:808/commonapi/acsystem/newerpapi/';
    const SERVER_NAME='http://account.kokoerp.com/commonapi/acsystem/newerpapi/';
    public static function getClient($uri='')
    {
        $server_url=self::SERVER_NAME.$uri;

        $client = new PHPRPC_Client();
        $client->useService($server_url);

        return $client;
    }

    /**
     * @param        $sql
     * @param string $db
     * @return mixed
     * 执行供应商系统SQl
     */
    public static function executeAccountSysSql($sql, $db = 'accountsystem')
    {
        $client=self::getClient('executeSql');
        return json_decode($client->getAccountSystemDataBySql($sql, $db), true);
    }


    public static function getAccounts($start_time,$end_time)
    {
        $client=self::getClient('putAccounts');
        $datas=$client->getData($start_time,$end_time);
        self::doAction($datas,'payaccountinfo');
    }

    public static function getAccountSystemDataBySql($sql) {
        $client=self::getClient('executeSql');
        return json_decode($client->getAccountSystemDataBySql($sql), true);
    }


    public static function getPayways($start_time,$end_time)
    {
        $client=self::getClient('putPayways');
        $datas=$client->getData($start_time,$end_time);
        self::doAction($datas,'payways');
    }

    /**
     * 找出需要处理的数据
     * @param array  $ids
     * @param string $table
     * @return array
     */
    static function dealIds(array $ids = NULL,$table)
    {
        if($ids){
            $dealIds = array();
            $exist_ids = DB::select('id')->from($table)
                ->execute('newerp')
                ->as_array(NULL,'id');
            if($exist_ids){
                $dealIds['m'] = array_intersect($exist_ids,$ids);//供应商系统中修改过的数据
                $dealIds['i'] = array_diff($ids, $exist_ids);//供应商系统中新增的数据
            }else{
                $dealIds['i'] = $ids;
            }
            return $dealIds;
        }
    }

    /**
     * 处理需要跟新的数据
     * @param array $ids
     * @param array $data_arr
     * @param       $table
     */
    static function doUpdate(array $ids = NULL,array $data_arr = NULL,$table)
    {
        if($ids&&$data_arr){
            foreach($data_arr as $data){
                if(in_array($data['id'],$ids)){
                    if($table=='payways'){
                        if($data['status']==1){
                            $data['status'] = 0;
                        } else{
                            $data['status'] = 1;
                        }
                    }
                    DB::update($table)
                        ->set($data)
                        ->where('id','=',$data['id'])
                        ->execute('newerp');
                }else{
                    continue;
                }
            }
        }
    }

    /**
     * 处理需要新增的数据
     * @param array $ids
     * @param array $data_arr
     * @param       $table
     * @throws Gleez_Exception
     */
    static function doInsert(array $ids = NULL,array $data_arr = NULL,$table)
    {
        if($ids&&$data_arr){
            foreach($data_arr as $data){
                if(in_array($data['id'],$ids)){
                    if($table=='payways'){
                        if($data['status']==1){
                            $data['status'] = 0;
                        } else{
                            $data['status'] = 1;
                        }
                    }
                    DB::insert($table,array_keys($data))
                        ->values(array_values($data))
                        ->execute('newerp');
                }else{
                    continue;
                }
            }
        }
    }

    /**
     * 执行同步操作
     * @param array $res
     * @param       $table
     * @throws ErrorException
     */
    static function doAction(array $datas = NULL,$table)
    {
        if($datas){
            $dealIds = self::dealIds(array_keys($datas),$table);//获取需要处理的数据
            //更新修改过数据
            if(isset($dealIds)&&$dealIds['m']){
                self::doUpdate($dealIds['m'],$datas,$table);
            }
            //插入新增的数据
            if(isset($dealIds)&&$dealIds['i']){
                self::doInsert($dealIds['i'],$datas,$table);
            }
        }
    }
}